﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class AccTestScript : MonoBehaviour
{
    Queue<float> accQueue = new Queue<float>();
    Queue<float> compassQueue = new Queue<float>();

    float gForce = 0.98f;

    bool isSfxPlayed = false;

    AudioClip[] clips;
    //int[] clipsRatio = new int[] { 3, 3, 3, 3, 1, 1 };
    int[] clipsRatio = new int[] { 3, 3, 3 };

    public AudioClip[] killAudio;

    public new Camera camera;


    void Start()
    {
        var audioSource = GetComponent<AudioSource>();
        if (audioSource != null)
        {
            clips = new AudioClip[clipsRatio.Length];
            for (int i=0; i<clips.Length; i++) clips[i] = Resources.Load<AudioClip>("SFX/NGJ" + i);
        }

        Screen.sleepTimeout = SleepTimeout.NeverSleep;
    }

    void Update()
    {
        accQueue.Enqueue(Vector3.Distance(Input.acceleration, new Vector3(0.0f, 0.0f, 0.0f)) - gForce);
        if (accQueue.Count > 5) accQueue.Dequeue();

        compassQueue.Enqueue(Input.compass.trueHeading);
        if (compassQueue.Count > 10) compassQueue.Dequeue();

        var text = GetComponent<Text>();
        if (text != null) text.text = accQueue.Max().ToString();

        var image = GetComponent<Image>();
        if (image != null) image.enabled = isSfxPlayed;

        if (camera != null && EnemyAudio.instance.AnyEnemy()) camera.transform.rotation = Quaternion.AngleAxis(Input.compass.trueHeading, Vector3.up);

        bool isActionTriggered = (accQueue.Max() >= gForce * 3.0f);
        if (isActionTriggered && !isSfxPlayed)
        {
            var audioSource = GetComponent<AudioSource>();
            if (audioSource != null)
            {
                float force = accQueue.Max() / (gForce * 3.0f);
                bool isKill = Attack(force);
                image.color = isKill ? Color.red : Color.yellow;
            }

            isSfxPlayed = true;
        }

        isSfxPlayed &= isActionTriggered;
    }

    bool Attack(float force)
    {
        var audioSource = GetComponent<AudioSource>();
        float distance = Vector3.Distance(EnemyAudio.instance.transform.position, new Vector3(0, 0, 0));

        if (camera != null) camera.transform.rotation = Quaternion.AngleAxis(compassQueue.Peek(), Vector3.up);
        float angle = Vector3.Angle(camera.transform.forward, EnemyAudio.instance.randomVec);

        //if (distance <= 10.0f && Math.Abs(angle) <= 22.5f)       // hit
        if (EnemyAudio.instance.AnyEnemy() && Math.Abs(angle) <= 22.5f)       // hit
        {
            audioSource.pitch = Math.Max(1.0f, Math.Min(2.0f, force / 1.5f));
            audioSource.PlayOneShot(killAudio[1]);

            EnemyAudio.instance.Stop();
            StartCoroutine(EnemyAudio.instance.DelayNewEnemy(5));
            return true;
        }
        else
        {
            audioSource.pitch = Math.Max(1.0f, Math.Min(3.0f, force));

            int idx = 0;
            for (int rnd = Random.Range(0, clipsRatio.Sum()); rnd >= clipsRatio[idx]; rnd -= clipsRatio[idx++]);
            audioSource.PlayOneShot(clips[idx]);
            return false;
        }
    }
}
